package chpt05;
import java.util.Arrays;
// Person类代码
class Person implements Comparable<Person>{
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "name=" + name + " age=" + age;
	}
	@Override
	public int compareTo(Person o) {
		return this.age - o.age; // 注意：不能处理o为null的情况
	}
}
// ComparableSortTest类代码
public class ComparableSortTest {
	public static void main(String[] args) {
		Person[] persons = new Person[3];
		persons[0] = new Person("zhang", 25);
		persons[1] = new Person("chen", 30);
		persons[2] = new Person("wang", 18);
		System.out.println(Arrays.toString(persons));
		Arrays.sort(persons); // 排序
		System.out.println(Arrays.toString(persons));
	}
}
